#!/bin/sh

echo "Configure new website - v0.1"

if [ "$(id -u)" != "0" ]; then
   echo
   echo " !! This script must be run as root" 1>&2
   echo
   exit 1
fi

if [ $# -lt 2 -o $# -gt 3 ];
then
  echo " Usage: `basename $0` <domain> <tld>"
  echo
  echo " To set up the domain dannyfine.nl, you'd enter:"
  echo "   [root@apollo ~]# `basename $0` dannyfine nl"
  echo
  exit -1
fi

hostname=$1
hosttld=$2
                            
fullhost="$hostname.$hosttld"
echo " - Setting up configuration for $fullhost"

echo " - Creating Apache virtual host configuration"
cp /etc/httpd/vhost.d/apollo.df.vc /etc/httpd/vhost.d/$fullhost

echo " - Creating folder for website and setting ownership to [danny]"
mkdir /var/www/sites/$fullhost
chown danny:danny /var/www/sites/$fullhost

echo " - Configuring Apache virtual host"
sed -i -i s/apollo\.df\.vc/$hostname\.$hosttld/ /etc/httpd/vhost.d/$fullhost
cat $fullhost | sed -i "/DocumentRoot \/var\/www/c \
	DocumentRoot \/var\/www\/sites\/$fullhost" /etc/httpd/vhost.d/$fullhost

echo " - Apache configured in /etc/httpd/vhost.d/$fullhost"
                                        
echo " - Creating AWStats configuration"
cp /etc/awstats/awstats.apollo.df.vc.conf /etc/awstats/awstats.$fullhost.conf

echo " - Configuring AWStats"
sed -i -i s/apollo\.df\.vc/$hostname\.$hosttld/ /etc/awstats/awstats.$fullhost.conf

echo " - AWStats configured in /etc/awstats/awstats.$fullhost.conf"

echo " - Restarting webserver"
service httpd restart
echo ""
echo " All done!"
echo

